package com.sywb.chuangyebao.view.fragment;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;

import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.bean.MediaStoreInfo;
import com.sywb.chuangyebao.contract.PublishVideoFromPhotoContract;
import com.sywb.chuangyebao.view.PublishVideoActivity;
import com.sywb.chuangyebao.view.PublishVideoEditActivity;

import org.bining.footstone.rxjava.rxbus.annotation.Subscribe;
import org.bining.footstone.rxjava.rxbus.annotation.Tag;
import org.bining.footstone.rxjava.rxbus.thread.ThreadMode;

/**
 * 通过相册发布视频
 * Created by duxl on 2018/5/17.
 */

public class PublishVideoFromPhotoFragment extends BaseStatisticsFragment<PublishVideoFromPhotoContract.Presenter> implements PublishVideoFromPhotoContract.View {

    public static final String TAG = "PublishVideoFromPhotoFragment";

    private TextView mTvNext;
    private ImageView mIvPlay;

    /**
     * Fragment 实例
     */
    public static PublishVideoFromPhotoFragment newInstance(Object... pramars) {
        PublishVideoFromPhotoFragment fragment = new PublishVideoFromPhotoFragment();
        fragment.setArguments(getBundle(pramars));
        return fragment;
    }

    @Override
    public int getLayoutId(Bundle bundle) {
        return R.layout.fragment_publish_video_from_photo;
    }


    @Override
    public void initView(View view, Bundle bundle) {
        super.initView(view, bundle);
        getView(R.id.ivClose_fragment_publish_video_from_photo).setOnClickListener(this);
        mIvPlay = getView(R.id.ivPlay_layout_v_play_control);
        mIvPlay.setOnClickListener(this);

        mTvNext = getView(R.id.tvNext_fragment_publish_video_from_photo);
        mTvNext.setOnClickListener(this);
        mTvNext.setVisibility(View.GONE);
    }

    @Override
    public void initPresenter() {
        mPresenter.initPresenter(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (!isCanClick(v)) return;
        if (v.getId() == R.id.ivClose_fragment_publish_video_from_photo) { // 关闭
            exit();

        } else if (v.getId() == R.id.tvNext_fragment_publish_video_from_photo) { // 下一步
            if (mPresenter.isPlaying()) {
                mPresenter.pause();
            }

            MediaStoreInfo mediaStoreInfo = mPresenter.getSelectedMedia();
            if (mediaStoreInfo != null) {
                advance(PublishVideoEditActivity.class, mediaStoreInfo.path, 0, (int) mediaStoreInfo.duration / 1000);
            }

        } else if (v.getId() == R.id.ivPlay_layout_v_play_control) { // 播放、暂停
            if (mPresenter.isPlaying()) {
                mPresenter.pause();
            } else {
                mPresenter.play();
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (((PublishVideoActivity) getActivity()).getTabPosition() == 0) {
            //getSurfaceView().setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        mPresenter.pause();
    }

    @Subscribe(thread = ThreadMode.MAIN_THREAD, tags = {@Tag(TAG)})
    public void onVisibleChange(String visible) {
        if ("onPause".equals(visible)) {
            onPause();
        } else if ("onResume".equals(visible)) {
            onResume();
        }
    }

    @Subscribe(thread = ThreadMode.MAIN_THREAD, tags = {@Tag(Constants.POST_VIDEO_SAVE)})
    public void onPublishSuccess(String data) { // 视频发布成功
        if (((PublishVideoActivity) getActivity()).getTabPosition() == 0) { // 判断Tab是在当前页面，避免两个tab都执行exit
            exit();
        }
    }

    @Override
    public RecyclerView getRecyclerView() {
        return getView(R.id.recyclerView_fragment_publish_video_from_photo);
    }

    @Override
    public SurfaceView getSurfaceView() {
        return getView(R.id.surfaceView_fragment_publish_video_from_photo);
    }

    @Override
    public ProgressBar getProgressBar() {
        return getView(R.id.pbProgress_fragment_publish_video_from_photo);
    }

    @Override
    public SeekBar getSeekBar() {
        return getView(R.id.sbProgress_layout_v_play_control);
    }

    @Override
    public TextView getCurrentPositionView() {
        return getView(R.id.tvCurrentPosition_layout_v_play_control);
    }

    @Override
    public TextView getTotalDurationView() {
        return getView(R.id.tvTotalDuration_layout_v_play_control);
    }

    @Override
    public void setNoData(boolean isNoData) {
    }

    @Override
    public void showNextBtn(int visible) {
        mTvNext.setVisibility(visible);
    }

    @Override
    public ImageView getIvPlay() {
        return mIvPlay;
    }

    @Override
    public boolean useRxBus() {
        return true;
    }
}
